Skip to content

Fix stability of Definition entities#1253

Open
lionel- wants to merge 14 commits into
oak/salsa-16-document-wire-urlfrom
oak/salsa-17-interning
Open

Fix stability of Definition entities#1253
lionel- wants to merge 14 commits into
oak/salsa-16-document-wire-urlfrom
oak/salsa-17-interning

Conversation

@lionel-

@lionel- lionel- commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Branched from #1252
Progress towards #1212

Like in ty, our Definitions are a tracked struct with some degree of stability across edits. Unlike in ty, the definitions are not created within the semantic index, they are created outside so that we keep the index salsa-free, which allows it to be easily reused to figure out data flow dependencies in non-salsa contexts.

Because the definitions are created outside the index we need to be a bit careful. This is a tracked struct, not an interned one, and that means that different Salsa queries create different instances of the definition that don't compare equal, even if they are the same definition. (See https://hackmd.io/5ddaZLKYSVC_YTTD3SzRDw?view#Tracked-Structs, thanks to @DavisVaughan for finding this doc.)

To ensure definitions have a single identity, we now funnel all definitions lookup through a single File::definitions() query that creates a map of Definition keyed by Scope and DefId (bundled in a new struct DefinitionSite).

New tests ensure that definitions are not invalidated when they shift in a file or when definitions are inserted before (which changes the lookup key but not the definition ID, so downstream queries remain valid even if their key is now outdated).

@DavisVaughan DavisVaughan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

pub(crate) fn definition(
self,
db: &'db dyn Db,
scope: ScopeId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scope: ScopeId,
scope_id: ScopeId,

consistency with def_id and other usages

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should pass through to DefinitionSite too i think


/// Every `Definition` in a file, keyed by its definition site.
#[derive(Debug, PartialEq, Eq, salsa::Update)]
struct FileDefinitions<'db> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct FileDefinitions<'db> {
struct Definitions<'db> {

would also be happy with this since the method is definitions()

lionel- added 14 commits June 10, 2026 16:09
Only canonicalise when comparing against paths from external sources
(e.g. normalised by R). Canonicalisation may corrupt the identity of
paths sent by frontends, and may prevent matching files at all when they
are deleted (at which point a non-canonicalised path sent by the
frontend can't be matched against canonicalised paths we've stored as
keys)
@lionel- lionel- force-pushed the oak/salsa-16-document-wire-url branch from 14bacca to 7743983 Compare June 10, 2026 14:32
@lionel- lionel- force-pushed the oak/salsa-17-interning branch from 0cd9628 to 25c9de8 Compare June 10, 2026 14:33
@lionel- lionel- force-pushed the oak/salsa-16-document-wire-url branch from 7743983 to a9b0bf2 Compare June 11, 2026 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants